// pages/ai/ai.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tempFile: '../../images/ya.jpg',
        tempFileData: '',
        compressedImage: ''
    },
    getAI: function () {
        this.chooseImage()
    },


    //选择图片
    chooseImage: function () {
        var that = this;
        wx.chooseImage({
            count: 9,
            sizeType: ["original", "compressed"],
            sourceType: ["album", "camera"],
            success: function (res) {
                that.setData({
                    tempFileData: wx.getFileSystemManager().readFileSync(res.tempFilePaths[0], "base64")
                });
                that.setData({
                    tempFile: 'data:image/jpg;base64,' + that.data.tempFileData
                });
                wx.request({
                    url: 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/classification/aixmbh?access_token=24.ce52e45e22fcbdd289a8d284a29bf1d2.2592000.1652949242.282335-25991114',
                    method: "POST",
                    header: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        "image": that.data.tempFileData,
                    },
                    success: res => {
                        console.log(res)
                        console.log(res.data.results)
                        var max=0,flag
                        res.data.results.forEach(item => {
                            if(item.score>=max) {
                                max=item.score
                                flag=item.name
                            }
                        })
                        console.log('识别结果',flag)
                    }
                    
                })

            }
        })
    },

    getToker: function () {
        console.log("getToken")
        wx.request({
            url: 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Ndrqn7iwAH7w9NGnr2aXahpW&client_secret=3j9cpfOzp4TsoQrgK6NtBgw76iV6tOGE',
            success: res => {
                console.log(res)
                console.log(res.data.access_token)
            }
        })
    },
    //压缩图片
    compressImage: function () {
        var that = this;
        wx.compressImage({
            src: that.data.tempFile[0],
            quality: 40,
            success(res) {
                console.log("压缩");
                var temp = res.tempFilePath;
                wx.saveImageToPhotosAlbum({
                    filePath: temp,
                    success(res) {
                        console.log("保存成功");
                        console.log(res);
                    }
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})